package com.fire.eyes.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fire.eyes.dao.TMitMarketImageMapper;
import com.fire.eyes.dao.TMitThemeGoodsMapper;
import com.fire.eyes.dao.TMitThemeMapper;
import com.fire.eyes.model.TMitTheme;
import com.fire.eyes.model.TMitThemeGoods;
import com.fire.eyes.util.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.core.util.ToolUtil;
import com.fire.eyes.service.ITMitThemeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 移保通专区表 服务实现类
 * </p>
 *
 * @author raoyuxing
 * @since 2018-04-28
 */
@Service
public class TMitThemeServiceImpl extends ServiceImpl<TMitThemeMapper, TMitTheme> implements ITMitThemeService {
	private static final Logger LOGGER = LoggerFactory.getLogger(TMitThemeServiceImpl.class);

	@Autowired
	private TMitThemeMapper themeMapper;
	@Autowired
	private TMitThemeGoodsMapper themeGoodsMapper;
	@Autowired
	private ImageUtil imageUtil;
	@Autowired
	private TMitMarketImageMapper imageMapper;
	
	
	/**
	 * 获取专题theme
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map getheme(Map map) {
    	LOGGER.debug("TMitThemeServiceImpl.getheme获取到的map消息" + map);
		//获取渠道或主题下的所有专区
    	LOGGER.debug("TMitThemeServiceImpl.getheme获取渠道或主题下的所有专区");
		EntityWrapper<TMitTheme> themeWrapper = new EntityWrapper<>();
		String shelfid = "shelfid";
		if (ToolUtil.isNotEmpty(map.get(shelfid))) {
			themeWrapper.eq("sourcetype", 2)
			.eq("sourceid", map.get("shelfid"));
		}else {
			themeWrapper.eq("sourcetype", 1)
			.eq("sourceid", map.get("chanelid"));
		}
		List<TMitTheme> themeList = themeMapper.selectList(themeWrapper);

		Map result = new HashMap<>(1);
		result.put("themelist", themeList);
		return result;
	}

	/**
	 * 获取专题theme2
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map getTheme2(Map map) {
		LOGGER.debug("TMitThemeServiceImpl.getTheme2获取到的map消息" + map);
		//获取渠道或主题下的所有专区
		LOGGER.debug("TMitThemeServiceImpl.getTheme2获取渠道或主题下的所有专区");
		EntityWrapper<TMitTheme> themeWrapper = new EntityWrapper<>();
		String shelfid = "shelfid";
		if (ToolUtil.isNotEmpty(map.get(shelfid))) {
			themeWrapper.eq("sourcetype", 2)
					.eq("sourceid", map.get("shelfid"));
		}else {
			themeWrapper.eq("sourcetype", 1)
					.eq("sourceid", map.get("chanelid"));
		}
		List<TMitTheme> themeList = themeMapper.selectList(themeWrapper);

		//获取每个专区下的所有商品信息
		LOGGER.debug("TMitThemeServiceImpl.getTheme2获取每个专区下的所有商品信息");
		List list = new ArrayList<>();
		for (TMitTheme tMitTheme : themeList) {
			Map themes = new HashMap<>(3);
			List<Object> themeimagelist = queryImageLinkByThemeId(tMitTheme.getThemeid());
			themes.put("themeid", tMitTheme.getThemeid());
			themes.put("themename", tMitTheme.getThemeidname());
			themes.put("themeimagelist", themeimagelist);
			list.add(themes);
		}

		Map result = new HashMap<>(1);
		result.put("themelist", list);
		return result;
	}

	/**
	 * 通过专区ID获取到该专区的所有imageurl
	 * @param themeId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object> queryImageLinkByThemeId (Integer themeId){

		LOGGER.debug("TMitThemeServiceImpl.queryImageByThemeId获取到的消息"+themeId);

		List<Object> themeimagelist = new ArrayList<>();
		EntityWrapper<TMitThemeGoods> themeGoodsWrapper = new EntityWrapper<>();
		themeGoodsWrapper.eq("themeid", themeId).eq("status", 2);

		List<TMitThemeGoods> themeGoodsList = themeGoodsMapper.selectList(themeGoodsWrapper);

		return themeimagelist;
	}
}
